package com.bonuo.crm.service.impl;

import com.bonuo.crm.domain.*;
import com.bonuo.crm.mapper.*;
import com.bonuo.crm.service.ContactService;
import com.bonuo.crm.service.VisitService;
import com.ruoyi.common.annotation.DataScope;
import com.ruoyi.common.core.domain.AjaxResult;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import java.sql.SQLIntegrityConstraintViolationException;
import java.util.List;

@Service
public class VisitServiceImpl implements VisitService {
    @Autowired
    private VisitMapper visitMapper;
    @Autowired
    private ClientMapper clientMapper;
    @Autowired
    private FacultyMapper facultyMapper;
    @Autowired
    private ContactMapper contactMapper;
    @Autowired
    private AgentMapper agentMapper;


    @Override
    @DataScope(provinceAlias = {"sub"})
    public List<Visit> selectVisitList(Visit visit) {
        return visitMapper.selectVisitList(visit);
    }

    @Transactional
    @Override
    public Integer insertVisit(Visit visit) {
        //插入拜访记录
//        Integer contactId = addContactAndClientAgentGetContactId(visit);
//        if(contactId == null){
//            return 0;
//        }
//        visit.setContactId(contactId);
        Integer addVisit = visitMapper.insertVisit(visit);
        return addVisit;
    }

    @Override
    public Integer deleteVisitById(Integer id) {
        return visitMapper.deleteVisitById(id);
    }

    @Transactional
    @Override
    public Integer updateVisit(Visit visit) {
//        Integer integer = addContactAndClientAgentGetContactId(visit);
//        visit.setContactId(integer);
        Integer update = visitMapper.updateVisit(visit);
        return update;
    }


    public Integer addContactAndClientAgentGetContactId(Visit visit){
        Integer contactId = null;
        try {
            if(visit.getType() == 0){
                //客户
                //先查询客户在客户表中是否存在
                Integer clientId = clientMapper.selectClientByName(visit.getSchoolOrCompanyName());
                if(clientId == null){
                    //插入客户信息
                    Client client = new Client();
                    client.setProvince(visit.getProvince());
                    client.setName(visit.getSchoolOrCompanyName());
                    client.setCreateBy(visit.getCreateBy());
                    client.setUpdateBy(visit.getUpdateBy());
                    client.setIsImportant(visit.getClientIsImportant());
                    client.setBasis(String.valueOf(visit.getClientBasis()));
                    int rows = clientMapper.insertClient(client);
                    //获得自增ID
                    clientId = client.getId();
                }

                //判断当前客户下是否有院系信息
                Integer facultyId = facultyMapper.selectFacultyByClientNameAndFacultyName(clientId,visit.getFacultyName());
                if(facultyId == null){
                    //插入院系信息
                    Faculty faculty = new Faculty();
                    faculty.setName(visit.getFacultyName());
                    faculty.setClientId(clientId);
                    faculty.setCreateBy(visit.getCreateBy());
                    faculty.setUpdateBy(visit.getUpdateBy());
                    int rows = facultyMapper.insertFaculty(faculty);
                    //获得自增ID
                    facultyId = faculty.getId();
                }

                //判断当前院系下是否有联系人信息
                contactId = contactMapper.selectContactByFacultyIdAndContactNameAndPhone(facultyId,visit.getContactName(),visit.getPhone());
                if(contactId == null){
                    //以上客户院系插入完成后插入 联系人信息
                    Contact contact = new Contact();
                    contact.setName(visit.getContactName());
                    contact.setPhone(visit.getPhone());
                    contact.setJob(visit.getJob());
                    contact.setIsImportant(visit.getContactIsImportant());
                    contact.setBasis(String.valueOf(visit.getContactBasis()));
                    contact.setClientAgentId(facultyId);
                    contact.setType(visit.getType());
                    contact.setCreateBy(visit.getCreateBy());
                    contact.setUpdateBy(visit.getUpdateBy());
                    int rows = contactMapper.insertContact(contact);
                    //获得自增ID
                    contactId = contact.getId();
                }


            }
            if(visit.getType() == 1){
                //代理商
                //先查询当前代理商是否存在
                Integer agentId = agentMapper.selectAgentByName(visit.getSchoolOrCompanyName());
                if(agentId == null){
                    Agent agent = new Agent();
                    agent.setName(visit.getSchoolOrCompanyName());
                    agent.setProvince(visit.getProvince());
                    agent.setCreateBy(visit.getCreateBy());
                    agent.setUpdateBy(visit.getUpdateBy());
                    int rows = agentMapper.insertAgent(agent);
                    //获得自增ID
                    agentId = agent.getId();
                }

                //根据代理商查询出联系人
                contactId = contactMapper.selectContactByAgentIdAndFacultyAndContactNameAndPhone(agentId,visit.getFacultyName(),visit.getContactName(),visit.getPhone());
                if(contactId == null){
                    Contact contact = new Contact();
                    contact.setName(visit.getContactName());
                    contact.setFaculty(visit.getFacultyName());
                    contact.setJob(visit.getJob());
                    contact.setPhone(visit.getPhone());
                    contact.setType(visit.getType());
                    contact.setIsImportant(visit.getContactIsImportant());
                    contact.setBasis(String.valueOf(visit.getContactBasis()));
                    contact.setClientAgentId(agentId);
                    contact.setCreateBy(visit.getCreateBy());
                    contact.setUpdateBy(visit.getUpdateBy());
                    int rows = contactMapper.insertContact(contact);
                    //获得自增ID
                    contactId = contact.getId();
                }
            }
            return contactId;
        }catch (Exception e){
            System.out.println(e.getMessage());
            e.printStackTrace();
            return null;
        }
    }
}
